package com.lianzhihui.romeex.database;

import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.database.data.Rm_Wallet;
import com.lianzhihui.romeex.database.utils.BaseTaction;
import com.lianzhihui.romeex.inter.AllWalletListener;
import com.lianzhihui.romeex.inter.AsListener;
import com.lianzhihui.romeex.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

import io.realm.RealmResults;

/**
 * Copyright (C), 2019-2020
 * FileName: BataBaseManager
 * Author: ZhaoFeng
 * Date: 2020/10/22 17:44
 * Description: 数据库增删改查入口
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
public class DataBaseManager {

    /**
     * 数据库数据转实体类
     *
     * @param wallet
     * @return
     */
    public static WalletInfo db2Bean(Rm_Wallet wallet) {
        if (wallet == null) {
            return null;
        }
        WalletInfo walletInfo = new WalletInfo();
        walletInfo.setUserName(wallet.getUserName());
        walletInfo.setAddress(wallet.getAddress());
        walletInfo.setSaftyCode(wallet.getSaftyCode());
        walletInfo.setMnemonicWords(wallet.getMnemonicWords());
        walletInfo.setPhone(wallet.getPhone());
        walletInfo.setCompressedPubkey(wallet.getPublicKey());
        walletInfo.setCompressedPrivkey(wallet.getPrivateKey());
        walletInfo.setUserUuid(wallet.getUserUuid());
        walletInfo.setHeadPathUrl(wallet.getHeadPathUrl());
        walletInfo.setCiphertext(wallet.getCiphertext());
        return walletInfo;
    }

    /**
     * ProjectName: NBN
     * ClassName: BataBaseManager
     * Description: 添加新的钱包
     * Author: 赵峰（zhaofengcto@foxmail.com）
     * CreateDate: 2020/10/22 17:55
     * UpdateUser: 更新者
     * UpdateDate: 2020/10/22 17:55
     * UpdateRemark: 更新说明
     * Version: 1.0
     */
    //TODO: 方法名命名规范:ADD_XXX；UP_XXX；DEL_XXX
    public static void add_WalletToDb(WalletInfo walletInfo) {

        new BaseTaction(DataBaseConfig.getRealm()) {
            @Override
            public void beginDb() {
                Rm_Wallet rmWallet = new Rm_Wallet();
                rmWallet.setAddress(walletInfo.getAddress());
                rmWallet.setUserName(walletInfo.getUserName());
                rmWallet.setSaftyCode(walletInfo.getSaftyCode());
                rmWallet.setMnemonicWords(walletInfo.getMnemonicWords());
                rmWallet.setPhone(walletInfo.getPhone());
                rmWallet.setPublicKey(walletInfo.getCompressedPubkey());
                rmWallet.setPrivateKey(walletInfo.getCompressedPrivkey());
                rmWallet.setUserUuid(walletInfo.getUserUuid());
                rmWallet.setHeadPathUrl(walletInfo.getHeadPathUrl());
                rmWallet.setCiphertext(walletInfo.getCiphertext());
                realm.copyToRealmOrUpdate(rmWallet);
            }
        };

    }

    /**
     * 修改钱包数据
     *
     * @param walletInfo
     */
    public static void up_WalletToDb(WalletInfo walletInfo) {

        new BaseTaction(DataBaseConfig.getRealm()) {
            @Override
            public void beginDb() {
                Rm_Wallet wallet = realm.where(Rm_Wallet.class).equalTo("address",walletInfo.getAddress()).findFirst();
                wallet.setUserName(walletInfo.getUserName());
                wallet.setSaftyCode(walletInfo.getSaftyCode());
                wallet.setMnemonicWords(walletInfo.getMnemonicWords());
                wallet.setPhone(walletInfo.getPhone());
                wallet.setPublicKey(walletInfo.getCompressedPubkey());
                wallet.setPrivateKey(walletInfo.getCompressedPrivkey());
                wallet.setUserUuid(walletInfo.getUserUuid());
                wallet.setHeadPathUrl(walletInfo.getHeadPathUrl());
                wallet.setCiphertext(walletInfo.getCiphertext());
                realm.copyToRealmOrUpdate(wallet);
            }
        };
    }

    /**
     * 异步获取所有钱包
     *
     * @param listener
     */
    public void getAllUser(AllWalletListener listener) {
        new BaseTaction(DataBaseConfig.getRealm()) {
            @Override
            public void beginDb() {
                RealmResults<Rm_Wallet> wallets = realm.where(Rm_Wallet.class)
                        .findAllAsync();
                listener.onCheck(wallets);
            }
        };
    }

    /**
     * 同步获取所有钱包
     */
    public static RealmResults<Rm_Wallet> getAllUser() {
        return DataBaseConfig.getRealm().where(Rm_Wallet.class).findAll();
    }

    /**
     * 异步获指定钱包
     *
     * @param listener
     */
    public static void getUser(String address, AsListener listener) {
        new BaseTaction(DataBaseConfig.getRealm()) {
            @Override
            public void beginDb() {
                Rm_Wallet wallet = realm.where(Rm_Wallet.class)
                        .equalTo("address", address)
                        .findFirstAsync();
                listener.onGetUserSuccess(db2Bean(wallet));
            }
        };
    }

    /**
     * 同步获取指定钱包
     *
     * @return
     */
    public static WalletInfo getUser(String address) {
        Rm_Wallet rmb = DataBaseConfig.getRealm().where(Rm_Wallet.class).equalTo("address", address).findFirst();
        if(rmb == null){
            return null;
        }
        return db2Bean(rmb);
    }

    /**
     * 同步获取指定钱包
     *
     * @return
     */
    public static Rm_Wallet getWallet(String address) {
        Rm_Wallet rmb = DataBaseConfig.getRealm().where(Rm_Wallet.class).equalTo("address", address).findFirst();
        if(rmb == null){
            return null;
        }
        return rmb;
    }

    /**
     * 删除 钱包
     */
    public static void del_WalletToDbSnyc(String address){

        new BaseTaction(DataBaseConfig.getRealm()) {
            @Override
            public void beginDb() {
                Rm_Wallet wallet = realm.where(Rm_Wallet.class)
                        .equalTo("address", address)
                        .findFirstAsync();
                wallet.deleteFromRealm();
            }
        };
    }

}
